home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Applications / QuArK / quarkpy / mapbrushnum.py < prev    next >
Text File  |  2004-01-05  |  4KB  |  150 lines

  1. """   QuArK  -  Quake Army Knife
  2.  
  3. Map editor brush-from-number finder
  4. """
  5. #
  6. # Copyright (C) 1996-2003 The QuArK community
  7. # THIS FILE IS PROTECTED BY THE GNU GENERAL PUBLIC LICENCE
  8. # FOUND IN FILE "COPYING.TXT"
  9. #
  10.  
  11. #$Header: /cvsroot/quark/runtime/quarkpy/mapbrushnum.py,v 1.2 2003/12/18 21:51:46 peter-b Exp $
  12.  
  13. import quarkx
  14. import qmacro
  15. import qtoolbar
  16. from maputils import *
  17. import mapeditor
  18.  
  19. #
  20. # an exception
  21. #
  22. corrupt = 'corrupt'
  23.  
  24. def findBrush(editor, address):
  25.     "address is a sequence of BrushAddressNodes"
  26.     current = editor.Root
  27.     for node in address:
  28.         if node.index==0:
  29.             quarkx.helppopup("This brush, named '%s', was generated by a duplicator or negative brush, whose containing group has been selected."%current.name)
  30.             return last
  31.         next = current.subitems[node.index-1]
  32.         if node.name!=next.name:
  33.             error="expected: %s; actual %s"%(node.name, next.name)
  34.             quarkx.msgbox("name discrepancy, "+error,
  35.                MT_ERROR,MB_OK)
  36.         last = current
  37.         current = next
  38.     return current
  39.         
  40.         
  41. class BrushNumDlg(SimpleCancelDlgBox):
  42.     #
  43.     # dialog layout
  44.     #
  45.     size = (160, 75)
  46.     dfsep = 0.6
  47.     flags = FWF_KEEPFOCUS
  48.     
  49.     dlgdef = """
  50.     {
  51.         Style = "9"
  52.         Caption = "Brush Number Dialog"
  53.  
  54.         brushnum: =
  55.         {
  56.         Txt = "Brush Number:"
  57.         Typ = "EF2"
  58.         Hint = "Enter entity and brush numbers, separated by space, as specified in compile" $0D "tool output (e.g. 0 2).  Brush will be selected on <enter> if possible." $0D "The use of duplicators may create difficulties."
  59.         }
  60.         cancel:py = {Txt="" }
  61.     }
  62.     """
  63.  
  64.     def __init__(self, form, editor, brushDict):
  65.     
  66.         src = quarkx.newobj(":")
  67.         self.src = src
  68.         self.editor = editor
  69.         self.brushDict = brushDict
  70.         SimpleCancelDlgBox.__init__(self,form,src)
  71.  
  72.     def ok(self):
  73.         pass
  74.  
  75.     def datachange(self, df):
  76.         entity, index = self.src["brushnum"]
  77.         entity, index = int(entity), int(index)
  78.         try:
  79.             address = self.brushDict[(entity,index)]
  80.         except (KeyError):
  81.             quarkx.msgbox("No such brush",MT_ERROR,MB_OK)
  82.             return
  83.         brush = findBrush(self.editor,address)
  84.         self.editor.layout.explorer.sellist = [brush]
  85.  
  86.  
  87. class BrushAddressNode:
  88.     ".name, .index (int)"
  89.     def __init__(self, nodeLabel):
  90.         name, index = nodeLabel.split('[')
  91.         self.name = name.strip()
  92.         index = index.strip()
  93.         index = index[:len(index)-1]
  94.         if index:
  95.             self.index = eval(index)
  96.         else:
  97.             self.index = 0
  98.  
  99. def getBrushDict(editor, filename):
  100.     f = open(filename, "r")
  101.     data = f.readlines()
  102.     f.close()
  103.     dict = {}
  104.     commentPrefix=quarkx.setupsubset()["MapCommentsPrefix"]
  105.     commentPrefixLen = len(commentPrefix)
  106.     i = -1
  107.     while 1:
  108.         i=i+1
  109.         if i>=len(data):
  110.             break
  111.         line = data[i]
  112.         if line[:commentPrefixLen]!=commentPrefix:
  113.             continue
  114.         linewords = line.split()
  115.         if linewords[1]=="Entity":
  116.             entity = eval(linewords[2])
  117.             brushCount = -1
  118.             continue
  119.         if linewords[1]!="Brush":
  120.             continue
  121.         brushNum = eval(linewords[2])
  122.         brushCount = brushCount+1
  123.         if brushNum!=brushCount:
  124.             raise corrupt, "Brush %d"%brushNum
  125.         i=i+1
  126.         line=data[i]
  127.         if line[:commentPrefixLen]!=commentPrefix:
  128.             quarkx.msgbox('problem with map file at line %d'%i,MT_ERROR,MB_OK)
  129.             continue
  130.         address = line[commentPrefixLen:].split('->')
  131.         address = map(BrushAddressNode, address)
  132.         dict[(entity, brushNum)] = address
  133.     return dict
  134.  
  135. def LoadBrushNums(editor, filename):
  136.     try:
  137.         brushDict = getBrushDict(editor, filename)
  138.         BrushNumDlg(quarkx.clickform, editor, brushDict)
  139.     except corrupt, info:
  140.         quarkx.helppopup("Brush number discrepancy at brush labelled %s; this probably means that the file is corrupt"%info)
  141.        
  142. #$Log: mapbrushnum.py,v $
  143. #Revision 1.2  2003/12/18 21:51:46  peter-b
  144. #Removed reliance on external string library from Python scripts (second try ;-)
  145. #
  146. #Revision 1.1  2003/03/24 10:34:24  tiglari
  147. #support for brush-number finder
  148. #
  149.  
  150.